package 题目集.dfs;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.LinkedList;
import java.util.List;

/**
 * dfs搜索
 */
//https://pintia.cn/problem-sets/1745607712521764864/exam/problems/1745607748311744522?type=7&page=0
public class 病毒溯源 {
    public static List<Integer>[] map;
    //    public static StringBuilder res=new StringBuilder();
    public static final StringBuilder empty = new StringBuilder();

    public static void main(String[] args) {
        Reader sc = new Reader();
        int n = sc.nextInt();
        map = new List[n];
        BitSet bitSet = new BitSet(n);
        for (int i = 0; i < n; i++) {
            int m = sc.nextInt();
            map[i] = new ArrayList<>(m);
            for (int j = 0; j < m; j++) {
                int to = sc.nextInt();
                bitSet.set(to);
                map[i].add(to);
            }
            map[i].sort(Integer::compareTo);
        }
        bitSet.flip(0, n);
        int s = bitSet.stream().iterator().nextInt();
        dfs(s);
        System.out.println(res.size());
        StringBuilder builder = new StringBuilder();
        res.forEach(o -> builder.append(o).append(" "));
        builder.deleteCharAt(builder.length() - 1);
        System.out.println(builder);
    }

    static LinkedList<Integer> path = new LinkedList<>();
    static LinkedList<Integer> res = new LinkedList<>();

    public static void dfs(int cur) {
        path.add(cur);
        if (map[cur].isEmpty() && res.size() < path.size()) {
            res = new LinkedList<>(path);
            path.removeLast();
            return;
        }
        for (Integer next : map[cur]) {
            dfs(next);
        }
        path.removeLast();
    }

    static class Reader {
        static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public static int nextInt() {
            try {
                in.nextToken();
            } catch (Exception ignored) {}
            return (int) in.nval;
        }
    }
}

